package cn.exrick.xboot.modules.Information.serviceimpl;

import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.Information.dao.mapper.RiverMapper;
import cn.exrick.xboot.modules.Information.entity.*;
import cn.exrick.xboot.modules.Information.service.IriverService;
import cn.exrick.xboot.modules.activiti.entity.ActBusiness;
import cn.exrick.xboot.modules.activiti.service.ActBusinessService;
import cn.exrick.xboot.modules.base.entity.Department;
import cn.exrick.xboot.modules.base.service.UserService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricIdentityLink;
import org.activiti.engine.history.HistoricTaskInstance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 河流信息接口实现
 *
 * @author Lyg
 */
@Slf4j
@Service
@Transactional
public class IriverServiceImpl extends ServiceImpl<RiverMapper, River> implements IriverService {

    @Autowired
    private RiverMapper riverMapper;


    @Override
    public List<RiverTourHistory> findRiverPeople(Map<String, Object> param) {
        return riverMapper.findRiverPeople(param);
    }

    @Override
    public List<WorkCondition> getWorkConditionForm(Map<String, Object> map) {
        return riverMapper.getWorkConditionForm(map);
    }

    @Override
    public List<RiverPatrol> getRiverPatrolForm(Map<String, Object> map) {
        List<RiverPatrol> riverList = riverMapper.getRiverPatrolForm(map);
        RiverPatrol riverPatrol = new RiverPatrol();
        riverPatrol.setDeptName("合计");
        int b = 0;

        int c = 0;

        int d = 0;

        Double e = 0.0;
//
        Double f = 0.0;
//
        int g = 0;
        for (RiverPatrol patrol : riverList) {
            String patrolNum = patrol.getPatrolNum();
            b += Integer.parseInt(patrolNum);
            String issueNum = patrol.getIssueNum();
            c += Integer.parseInt(issueNum);
            String personRiverNum = patrol.getPersonRiverNum();
            d += Integer.parseInt(personRiverNum);
            String personFindProblem = patrol.getPersonFindProblem();
            e += Double.parseDouble(personFindProblem);
            String personEndRate = patrol.getPersonEndRate();
            f += Double.parseDouble(personEndRate);
            g++;
        }
        String bb = "" + b;
        riverPatrol.setPatrolNum(bb);
        String cc = "" + c;
        riverPatrol.setIssueNum(cc);
        String dd = "" + d;
        riverPatrol.setPersonRiverNum(dd);
        if (c != 0 && d != 0) {
            Double c1 = c * 1.0;
            Double d1 = d * 1.0;
            NumberFormat nf = new DecimalFormat("#0.00");
            String format = nf.format(c1 / d1);
            //    int eee=  c/d;
            riverPatrol.setPersonFindProblem(format);
        } else {
            String p = "" + f;
            riverPatrol.setPersonFindProblem(p);
        }

        if (f != 0 && g != 0) {
            Double fff = f / g;
            String ff = "" + fff;
            String end = ff.substring(0, 4);
            riverPatrol.setPersonEndRate(end);
        } else {
            String end1 = "" + f;
            riverPatrol.setPersonEndRate(end1);
        }

        riverList.add(riverPatrol);
        return riverList;
    }

    @Override
    public List getUserId(Map<String, Object> map) {
        return riverMapper.getUserId(map);
    }

    @Override
    public List<RiverTourNum> getRiverTourNum(Map<String, Object> map) {
        List<RiverTourNum> list = new ArrayList<>();
        String s = riverMapper.getZhen(map);
        //判断是查询镇 还是 居委会
        if(s!=null){
            if (s.contains("镇") || s.contains("流域")) {
                list = riverMapper.getZhenListId(map);
            } else {
                list = riverMapper.getListId(map);
            }
        }else{
            list = riverMapper.getListId(map);
        }
        for (RiverTourNum riverTourNum : list) {
            //查询巡河的相关数据
//            String id = riverTourNum.getId();
//            map.put("id",id);
            if (riverTourNum != null) {
                String id = riverTourNum.getId();
                if (id != null) {
                    String xunhe = riverMapper.getXunHe(map, id);
//            map.remove(id);
                    riverTourNum.setPatrolNum(xunhe);
                    String km = riverMapper.getKm(map, id);
                    riverTourNum.setPatrolKm(km);
                    String kmi= riverMapper.getKmI(map,id);
                    riverTourNum.setPatrolNumI(kmi);
                    String num = riverMapper.getIssueNum(map, id);
                    riverTourNum.setIssueNum(num);
                    String finish = riverMapper.getFinish(map, id);
                    riverTourNum.setFinish(finish);
                    String yourself = riverMapper.getYourself(map, id);
                    riverTourNum.setYourself(yourself);
                    Object startDate = map.get("startDate");
                    Object endDate = map.get("endDate");
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String s1 = startDate.toString();
                    String s2 = endDate.toString();
                    String str = "";
                    try{
                        Date parse = simpleDateFormat.parse(s1);
                        Date parse1 = simpleDateFormat.parse(s2);
                        long time = (parse1.getTime()-parse.getTime())/(24*3600*1000);
                        Calendar  calendar = new GregorianCalendar();
                        for(int i = 0;i<=time;i++){
                            calendar.setTime(parse);
                            calendar.add(calendar.DATE,i);
                            String format = simpleDateFormat.format(calendar.getTime());
                            String s3= riverMapper.getDetails(format,id);
                            if(s3.equals("0")){
                                str+=format+", ";
                            }
                        }
                        if(str!=""){
                            riverTourNum.setDetails(str);
                            riverTourNum.setNotwork("否");
                        }else{
                            riverTourNum.setDetails("无");
                            riverTourNum.setNotwork("是");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
//                    String format = simpleDateFormat.format(startDate);
//                    String format1 = simpleDateFormat.format(endDate);
//                    System.out.println(format+"-----"+format1);
                }
//            System.out.println(riverTourNum.getId());
//            System.out.println(riverTourNum.getRegisterUser());
//            System.out.println(riverTourNum.getRegisterNum());
//            System.out.println(riverTourNum.getPatrolNum());
//            System.out.println(riverTourNum.getPatrolKm());
//            System.out.println(riverTourNum.getIssueNum());
            }
        }
        return list;
    }

    @Override
    public List<RiverTourNum> getRiverTourNumNo(Map<String, Object> map) {

        List<RiverTourNum> list = new ArrayList<>();
        String s = riverMapper.getZhen(map);
        if(s!=null){
            if (s.contains("镇") || s.contains("流域")) {
                list = riverMapper.getZhenListNoId(map);
            } else {
                list = riverMapper.getNoId(map);
            }
        }else{
            list = riverMapper.getNoId(map);
        }

        for (RiverTourNum riverTourNum : list) {
            if (riverTourNum != null) {
//            String id = riverTourNum.getId();
//            String xunhe =  riverMapper.getXunHe(id);
//            map.remove(id);
                riverTourNum.setPatrolNum("0");
//            String km =  riverMapper.getKm(id);
                riverTourNum.setPatrolKm("0");
//            String num= riverMapper.getIssueNum(id);
                riverTourNum.setIssueNum("0");
                riverTourNum.setPatrolNumI("0");
                riverTourNum.setRegisterNum("0");
                riverTourNum.setFinish("0");
                riverTourNum.setYourself("0");
                riverTourNum.setNotwork("否");
                riverTourNum.setDetails("未巡河");
            }
//            System.out.println("1");
//            for (Object key : map.keySet()) {
//            System.out.println("key:" + key + ",value:" + map.get(key));
//        }
        }

        return list;
    }

    @Override
    public List<RiverTourNum> getRiverTourNumByDept(Map<String, Object> map) {
//        赋值
        List<RiverTourNum> list =   riverMapper.getRiverTourNumByDept(map);
        String startDate = map.get("startDate").toString();
        String endDate = map.get("endDate").toString();
        String bgeinTime = startDate.substring(0, startDate.indexOf(" "));
        String endTime = endDate.substring(0, endDate.indexOf(" "));
        List<String> listData = getBetweenDate(bgeinTime,endTime);
        for (RiverTourNum r:list) {
            String strtime = "";
            for (String str:listData) {
                if(StringUtils.isNotEmpty(r.getPatrolNumITime()) && !r.getPatrolNumITime().contains(str)){
                    strtime+=str+",";
                }
            }
            if("".equals(strtime)){
                r.setDetails("无");
                if("0".equals(r.getPatrolNum())){
                    r.setNotwork("否");
                }else{
                    r.setNotwork("是");
                }
            }else{
                r.setDetails(strtime);
                r.setNotwork("否");
            }
        }
        return  list;
    }

    @Override
    public List<RiverTourNum> getNotRiverTourNumByDept(Map<String, Object> map) {
        return riverMapper.getNotRiverTourNumByDept(map);
    }

    @Override
    public String getDeptChilds(String deptIds) {
        return riverMapper.getDeptChilds(deptIds);
    }

    @Override
    public List<RiverPersonTable> getRiverPatrolTable(Map<String, Object> map) {
        List<RiverPersonTable> riverPatrolTable = riverMapper.getRiverPatrolTable(map);

        for (RiverPersonTable riverPersonTable : riverPatrolTable) {
            String id = riverPersonTable.getId();
            RiverPersonTable r= riverMapper.getRiver(map,id);
            String name = riverPersonTable.getDeptName();
            String n = riverMapper.getNotLog(map,id);
            String pn = riverMapper.getPn(map,id);
            String pv= riverMapper.getPv(map,id);
            String km = riverMapper.getDistances(map,id);
            String in = riverMapper.getIIssueNum(map,id);
            String num = r.getPersonRiverNum();
            int i = Integer.parseInt(num);
            int i1 = Integer.parseInt(in);
            String personFindProblem = "";
            if(i!=0&&i1!=0){
                DecimalFormat df = new DecimalFormat("0.00");//格式化小数
                personFindProblem = df.format((float)i1/i);//返回的是String类型
            }else {
                personFindProblem = "0.00";
            }
            Object startDate = map.get("startDate");
            Object endDate = map.get("endDate");
            //完成巡河任务人数
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String s1 = startDate.toString();
            String s2 = endDate.toString();
            List<String> listId= riverMapper.getUserIId(map,id);
            Integer tt=0;
            for (String s : listId) {
                Integer ll = 0;
                try{
                    Date parse = simpleDateFormat.parse(s1);
                    Date parse1 = simpleDateFormat.parse(s2);
                    long time = (parse1.getTime()-parse.getTime())/(24*3600*1000);
                    Calendar  calendar = new GregorianCalendar();
                    for(int k = 0;k<=time;k++){
                        calendar.setTime(parse);
                        calendar.add(calendar.DATE,i);
                        String format = simpleDateFormat.format(calendar.getTime());
                         Integer zhi = riverMapper.getNums(format,s);
                         if(zhi>0){
                             ll++;
                         }
                    }
                     if(ll>=time){
                         tt++;
                     }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            String ss = r.getPersonRiverNum();
            int integer = Integer.parseInt(ss);
            if(tt!=0 && integer!=0){
                DecimalFormat df = new DecimalFormat("0.00");//格式化小数
                String stff= df.format((float)tt/integer);//返回的是String类型
                riverPersonTable.setRiverTask(stff+"%");
            }else{
                riverPersonTable.setRiverTask("0%");
            }
            String riverPerson = tt.toString();
            riverPersonTable.setRiverPerson(riverPerson);
            riverPersonTable.setPatrolNumValid(pv);
            riverPersonTable.setPersonRiverNum(r.getPersonRiverNum());
            riverPersonTable.setNotLog(n);
            riverPersonTable.setPatrolNum(pn);
            riverPersonTable.setDistances(km);
            riverPersonTable.setIssueNum(in);
            riverPersonTable.setPersonFindProblem(personFindProblem);

        }
        if(riverPatrolTable!=null){
            RiverPersonTable riverPersonTable1 = new RiverPersonTable();
            riverPersonTable1.setDeptName("合计");
            int prn = 0;
            int ntl = 0;
            int ptn = 0;
            int pnv = 0;
            int dis = 0;
            int isn = 0;
            int rp  = 0;
            Double rt = 0.00;
            for (RiverPersonTable riverPersonTable : riverPatrolTable) {
                String personRiverNum = riverPersonTable.getPersonRiverNum();
                prn += Integer.parseInt(personRiverNum);
                String notLog = riverPersonTable.getNotLog();
                ntl += Integer.parseInt(notLog);
                String patrolNum = riverPersonTable.getPatrolNum();
                ptn += Integer.parseInt(patrolNum);
                String patrolNumValid = riverPersonTable.getPatrolNumValid();
                pnv += Integer.parseInt(patrolNumValid);
                String distances = riverPersonTable.getDistances();
                dis += Integer.parseInt(distances);
                String issueNum = riverPersonTable.getIssueNum();
                isn += Integer.parseInt(issueNum);
                String riverPerson = riverPersonTable.getRiverPerson();
                rp += Integer.parseInt(riverPerson);
                String riverTask = riverPersonTable.getRiverTask();
                String substring = riverTask.substring(0, riverTask.length() - 1);
                rt+=Double.valueOf(substring);
            }
            if(isn!=0&&prn!=0){
                DecimalFormat df = new DecimalFormat("0.00");//格式化小数
                String trr = df.format((float)isn/prn);//返回的是String类型
                riverPersonTable1.setPersonFindProblem(trr);
            }else {
                riverPersonTable1.setPersonFindProblem("0.0");
            }
            if(rt!=0){
                DecimalFormat df = new DecimalFormat("0.00");//格式化小数

                Double dd= rt/riverPatrolTable.size();
                String format = df.format(dd);
                riverPersonTable1.setRiverTask(format+"%");
            }else {
               riverPersonTable1.setRiverTask("0%");
            }
            String s1="";
            riverPersonTable1.setPersonRiverNum(s1+prn);
            String s2="";
            riverPersonTable1.setNotLog(s2+ntl);
            String s3 ="";
            riverPersonTable1.setPatrolNum(s3+ptn);
            String s4 ="";
            riverPersonTable1.setPatrolNumValid(s4+pnv);
            String s5="";
            riverPersonTable1.setDistances(s5+dis);
            String s6="";
            riverPersonTable1.setIssueNum(s6+isn);
            String s7 ="";
            riverPersonTable1.setRiverPerson(s7+rp);
            riverPatrolTable.add(riverPersonTable1);
        }


        return riverPatrolTable;
    }

    @Override
    public List<TownRiverTable> getOperationAll(Map<String, Object> map) {
        List<TownRiverTable> riverPatrolTable = riverMapper.getTownRiverTable(map);
        DecimalFormat df = new DecimalFormat("0.00");

        for (TownRiverTable townRiverTable : riverPatrolTable) {
            String id = townRiverTable.getId();
            String name = townRiverTable.getDeptName();
            //上报问题完成整改数
            String ino = riverMapper.getIssueNumOk(map,id);
            //上报问题数
            String in = riverMapper.getIIssueNum(map,id);
            int i = Integer.parseInt(in);
            int i1 = Integer.parseInt(ino);
            String personFindProblem ="";

            if(i!=0&&i1!=0){
                //格式化小数
                personFindProblem = df.format((float)i1/i);//返回的是String类型
                Double aDouble = Double.valueOf(personFindProblem);
                personFindProblem =""+ aDouble*100+"%";
            }else {
                personFindProblem="0%";
            }
            //第三方发现问题数
            String ti = riverMapper.getThreeIssue(map,id);
            //问题完成整改数
            String trn= riverMapper.getThreeRateNum(map,id);
            int o = Integer.parseInt(ti);
            int o1 = Integer.parseInt(trn);
            String tr = "";
            if(o!=0 && o1!=0){
                tr = df.format((float)o1/o);//返回的是String类型
                Double aDouble = Double.valueOf(tr);
                tr =""+ aDouble*100+"%";
            }else {
              tr = "0%";
            }
            townRiverTable.setIssueNum(in);
            townRiverTable.setIssueNumOk(ino);
            townRiverTable.setRate(personFindProblem);
            townRiverTable.setThreeIssue(ti);
            townRiverTable.setThreeRateNum(trn);
            townRiverTable.setThreeRate(tr);
        }
        TownRiverTable townRiverTable1 = new TownRiverTable();
        townRiverTable1.setDeptName("合计");
        int xgy = 0;
        int sb = 0;
        int ther = 0;
        int wc = 0;

        if(riverPatrolTable!=null){
            for (TownRiverTable townRiverTable : riverPatrolTable) {
                String issueNum = townRiverTable.getIssueNum();
                xgy+=Integer.parseInt(issueNum);
                String issueNumOk = townRiverTable.getIssueNumOk();
                sb+=Integer.parseInt(issueNumOk);
                String threeIssue = townRiverTable.getThreeIssue();
                ther+=Integer.parseInt(threeIssue);
                String threeRateNum = townRiverTable.getThreeRateNum();
                wc+=Integer.parseInt(threeRateNum);
            }
            if(xgy!=0&&sb!=0){
               String trr = df.format((float)sb/xgy);//返回的是String类型
                Double aDoubleq = Double.valueOf(trr);
                trr=""+aDoubleq*100;
                townRiverTable1.setRate(trr+"%");
            }else{
                townRiverTable1.setRate("0%");
            }
            if(ther!=0&&wc!=0){
                String trr = df.format((float)wc/ther);//返回的是String类型
                Double aDoubleq = Double.valueOf(trr);
                trr=""+aDoubleq*100;
                townRiverTable1.setThreeRate(trr+"%");
            }else {
                townRiverTable1.setThreeRate("0%");
            }
            String ss = "";
            townRiverTable1.setIssueNum(ss+xgy);
            String ss1="";
            townRiverTable1.setIssueNumOk(ss1+sb);
            String ss2 ="";
            townRiverTable1.setThreeIssue(ss2+ther);
            String ss3 = "";
            townRiverTable1.setThreeRateNum(ss3+wc);
            riverPatrolTable.add(townRiverTable1);
        }
        return riverPatrolTable;
    }

    public static List<String> getBetweenDate(String begin,String end){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<String> betweenList = new ArrayList<String>();
        String or = "";
        try {
            Date dt1 = format.parse(begin);
            Date dt2 = format.parse(end);
            if (dt1.getTime() > dt2.getTime()) {
                or = begin;
                begin = end;
                end = or;
            }
        } catch (ParseException e) { }
        try{
            Calendar startDay = Calendar.getInstance();
            startDay.setTime(format.parse(begin));
            startDay.add(Calendar.DATE, -1);

            while(true){
                startDay.add(Calendar.DATE, 1);
                Date newDate = startDay.getTime();
                String newend=format.format(newDate);
                betweenList.add(newend);
                if(end.equals(newend)){
                    break;
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return betweenList;
    }
    @Override
    public List<ParagraghOperation> getParagraghOperation(SearchVo searchVo) {
        ParagraghOperation operation = new ParagraghOperation();
        List<ParagraghOperation> paragraghOperation = riverMapper.getParagraghOperation(searchVo);
        Integer accomplish = 0;
        Integer pendingTrial = 0;
        Integer thirdParty = 0;
        Integer riverPatrol = 0;
        for(int i=0;i<paragraghOperation.size();i++){
            accomplish += paragraghOperation.get(i).getAccomplish();
            pendingTrial += paragraghOperation.get(i).getPendingTrial();
            thirdParty += paragraghOperation.get(i).getThirdParty();
            riverPatrol += paragraghOperation.get(i).getRiverPatrol();
        }
        operation.setTitle("合计");
        operation.setAccomplish(accomplish);
        operation.setPendingTrial(pendingTrial);
        operation.setThirdParty(thirdParty);
        operation.setRiverPatrol(riverPatrol);
        paragraghOperation.add(operation);
        return paragraghOperation;
    }
}